package edu.example;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class AddReviewDialog extends JDialog
{
	private Application currentApp;
	private Reviewer currentReviewer;
	private ReviewManager rm;
	
	// dialog GUI
	private JPanel mainPanel;
	private JLabel revieweeLabel;
	private JLabel reviewerLabel;
	private JLabel nameLabel;
	private JLabel ratingLabel;
	private JTextField ratingNumTextField;
	private JLabel reviewLabel;
	private JTextArea reviewTextArea;
	private JScrollPane reviewScrollPane;

	private JPanel buttonPanel;
	private JButton saveButton;
	private JButton cancelButton;
	
	// event Handler
	private OkHandler okHandler;
	private BackHandler backHandler;
	
	public AddReviewDialog(Application initApp, Reviewer initCurrent, ReviewManager initRM)
	{
		rm = initRM;
		currentApp = initApp;
		currentReviewer = initCurrent;
		this.setTitle("Add Review");
		this.setSize(720, 480);
		this.setResizable(false);
		layoutGUI();
	}

	public void layoutGUI()
	{
		// size and locate the dialog at the center of the screen
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
		revieweeLabel = new JLabel(
						"Add review for: "+
						currentApp.getApplicantLastName()+" "+
						currentApp.getApplicantFirstName());
		
		reviewerLabel = new JLabel("Reviewer: ");
		nameLabel = new JLabel(currentReviewer.getName());
		ratingLabel = new JLabel("Rating: ");
		ratingNumTextField = new JTextField(10);
		reviewLabel = new JLabel("Review: ");
		reviewTextArea = new JTextArea(15, 60);
		reviewTextArea.setWrapStyleWord(true);
		reviewTextArea.setLineWrap(true);
		reviewScrollPane = new JScrollPane(reviewTextArea);
		
		buttonPanel  = new JPanel();
		saveButton = new JButton("Save");
		cancelButton = new JButton("Cancel");

		mainPanel = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		mainPanel.setLayout(layout);
		
		addComponent(revieweeLabel,			mainPanel,	0,	0,	1,	1);
		addComponent(reviewerLabel,			mainPanel,	0,	1,	1,	1);
		addComponent(nameLabel,				mainPanel,	1,	1,	1,	1);
		addComponent(ratingLabel,			mainPanel,	0,	2,	1,	1);
		addComponent(ratingNumTextField,	mainPanel,	1,	2,	1,	1);
		addComponent(reviewLabel,			mainPanel,	0,	3,	1,	1);
		addComponent(reviewScrollPane,		mainPanel,	0,	4,	2,	5);
		
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);
		
		this.add(mainPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);

		
		okHandler = new OkHandler();
		backHandler = new BackHandler();
		saveButton.addActionListener(okHandler);
		cancelButton.addActionListener(backHandler);
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}

	class OkHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			currentReviewer.setCurrentApp(currentApp);
			Review rv = currentReviewer.writeReview();
			rv.setcontent(reviewTextArea.getText());
			try{
				rv.setRating(Double.parseDouble(ratingNumTextField.getText()));
			}
			catch (NumberFormatException nfe){
				JOptionPane.showMessageDialog(AddReviewDialog.this, "Error: Wrong Rating Data Type", "Error", JOptionPane.ERROR_MESSAGE);
			}
			rm.addReview(rv);
			AddReviewDialog.this.setVisible(false);
		}
		
	}
	class BackHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			AddReviewDialog.this.setVisible(false);
		}
		
	}
	
	public static void main(String[] args)
	{
		File file1 = new File("AY.txt");
		File file2 = new File("AY-map.txt");
		File file3 = new File("OTS.txt");
		File file4 = new File("OTS-map.txt");
		ApplicationManager formatter = new ApplicationManager(null, file1, file2, file3, file4);
		UserManager um = new UserManager();
		um.login("chair", "abc");
		ReviewManager rm  = new ReviewManager(um);
		AddReviewDialog temp3 = new AddReviewDialog(formatter.getAllApps().get("alan_perlis@amail.com"), (Reviewer)um.getCurrentUser(), rm);
		temp3.setVisible(true);
		
	}
}
